
package horde;

import horde.control.AutomatonBehavior;
import horde.minefield.andres.robots.robotControl;
import horde.minefield.andres.smartSmithTest;
import horde.minefield.andres.smithTest;
import horde.minefield.arturo.hcvisualTest;
import horde.patterns.Msg;
import horde.patterns.master.MasterAgent;
import horde.internalResources.HorrorControl.HorrorControl;
import horde.internalResources.Launcher;
import horde.internalResources.dictionaries.dictionary;
import horde.internalResources.editor.Editor;
import horde.internalResources.horrorControl.HCvis.tracesPanel;
import horde.minefield.arturo.methodsTest;
import horde.standard.resources.graphics.HFrame;
import horde.standard.resources.graphics.StackLayout;
import horde.standard.resources.graphics.TableLabel;
import horde.utils.ParseXML;
import horde.utils.ParseXML.Node;
import horde.utils.RawLoader;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


/**
 * @author Andres Picazo
 * @author Arturo Mazón
 */
public class Main {

    private static int test = 0; /* Horror test */
//    privateS int test = 1; /* Smith test */

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length == 1) test = Integer.parseInt(args[0]);
        switch (test) {
            case 0: testHorror(); break;
            case 1: testSmith(); break;
            case 2: testLauncher(); break;
            case 3: testBundle(); break;
            case 4: testXML(); break;
            case 5: testAutomaton(); break;
            case 6: testHCVTest();break;
            case 7: testHordeParser(); break;
            case 8: testStart(); break;
            case 9: testRobots(); break;
            case 10:testPanelCell(); break;
            case 11:testEditor(); break;
            case 12:testMethods();break;
            case 13:testStack(); break;
            case 14:testIndependent();break;
            case 15:testRobots2();break;
            case 16:testNetBots();break;
            case 17:testNetBotsSith();break;
        }
    }

    private static void testHorror () {        
        HorrorControl h = HorrorControl.getInstance("ES");
        h.trace("main", "Pruebas1", HorrorControl.ERROR_PLATFORM);
        h.trace("main", "Pruebas2", HorrorControl.TRACE_INFO);
        h.trace("main", "Pruebas3", HorrorControl.TRACE_INFO);
        h.export("File");
    }

    private static void testSmith () {
        System.out.println ("Testing smith...");
        smartSmithTest.test();
    }

    private static void testLauncher () {
        String [] messages = {"Starting master...", "Creating agents", "Agent smith created", "Agent Mr.Black created", 
        "Random shit", "Wasting time here...", "Doing some boring things..", "Done"};
        Launcher L = new Launcher("Launcher test",RawLoader.hordeSplash, Color.red, 220);
        try {
            for (String message : messages) {                
                L.showMessage(message);
                Thread.sleep(1500);
            } Thread.sleep(1000);
//            L.dispose();
            L.fadeAndDispose();
        } catch (Exception e) {
            System.err.println("Failure on test sleep");
        }
    }

    private static void testBundle(){  
        dictionary.setBundle("ES");
        String [] messages = {dictionary.getString("la.m1"),dictionary.getString("la.m2"),
                              dictionary.getString("la.m3"),dictionary.getString("la.m4"),
                              dictionary.getString("la.m5"),dictionary.getString("la.m6"),
                              dictionary.getString("la.m7"),dictionary.getString("la.m8")};
        Launcher L = new Launcher("Launcher test",RawLoader.hordeSplash, Color.red, 220);
        try {
            for (String message : messages) {
                L.showMessage(message);
                Thread.sleep(1500);
            } Thread.sleep(1000);
//            L.dispose();
            L.fadeAndDispose();
        } catch (Exception e) {
            System.err.println("Failure on test sleep");
        }
        Object[] a = {2,3};
        System.out.print(dictionary.getString("s1",a));
    }

    private static void testXML () {
        String location = "/horde/minefield/andres/automatonTest.xml";
        AutomatonBehavior ab = new AutomatonBehavior (location);
    }

    private static void testAutomaton() {
        smartSmithTest.test2();
    }

    private static void testHCVTest(){
        hcvisualTest.test();
    }

    private static void testHordeParser() {
        String location = "/horde/minefield/andres/automatonTest.xml";
//        ParseXML parser = new ParseXML(location);
//        Node root = parser.getParsedRoot();
        Node root = ParseXML.parseFile(location);
        if (root != null) System.out.println (root.toString());
    }

    private static void testStart () {
        String location = "/horde/minefield/andres/demoApp.xml";
        MasterAgent master = Launcher.start(location);
        Msg start = new Msg ("Tester", "Smith", Msg.TYPE_REQUEST, "start", new Integer(5));
//        the_architect.sendMessage(sayHi);
        master.sendMessage(start);
        try {Thread.sleep(2000);} catch (Exception e) {}
        System.err.println ("Starting agents...");
        /* Start the gunfire */
        master.startAgents();
        System.err.println ("Running " + master.getNumberOfAgentsRunning() + " agents");
        try {
            System.out.println ("Stopping for a couple seconds...");
            Thread.sleep(2000);
        } catch (Exception e) {
            System.err.println ("Failure stopping main thread");
        }
        /* Send the same message to check status */
        /* replicate the message so it gets a new number */
        start = new Msg ("Tester", "Smith", Msg.TYPE_REQUEST, "start", new Integer(5));
        master.sendMessage(start);
        try {
            System.out.println ("Stopping for a couple seconds...");
            Thread.sleep(1000);
        } catch (Exception e) {
            System.err.println ("Failure stopping main thread");
        }
        /* Send the correct message */
        Msg anotherMsg = new Msg ("Tester", "Smith", Msg.TYPE_REQUEST, "work");
        master.sendMessage(anotherMsg);
//        System.err.println ("Trying shared resource...");
//        resourceDemo rd = (resourceDemo) master.getSharedResource("demo");
//        if (rd != null) rd.mult(3,5);
//        /* Politely request smith to stop messing around */
//        try {Thread.sleep(3000);master.stopAgents();} catch (Exception e) {}

    }

    private static void testRobots () {
        /* Full test */
        String location = "/horde/minefield/andres/robots/robots.xml";
        MasterAgent master = Launcher.start(location);
        boolean agentsReady = master.startAgents();
        if (agentsReady) {
            robotControl rc = (robotControl) master.getSharedResource("robotControl");
            rc.setVisible(true);
        }
    }

    private static void testRobots2 () {
        /* Full test */
        String location = "/horde/minefield/andres/robots/robots2.xml";
        MasterAgent master = Launcher.start(location);
        boolean agentsReady = master.startAgents();
        if (agentsReady) {
            robotControl rc = (robotControl) master.getSharedResource("robotControl");
            rc.setVisible(true);
        }
    }
    

    private static void testPanelCell() {
//        PanelCell.test();
        HFrame f = new HFrame("Test cells", 400,300);
        /* Si queremos controlar como se renderizan todas las columnas de golpe */
        DefaultTableModel tm = new DefaultTableModel() {
            @Override
            public Class getColumnClass(int columnIndex) {
                if (columnIndex == 0) return Boolean.class;
                else return TableLabel.class;
            }
        };
        JTable t = new JTable();
        tm.addColumn("Valid");
        tm.addColumn("Data");
        t.setModel(tm);
        t.setDefaultRenderer(TableLabel.class, new TableLabel(Color.lightGray));
        t.getColumnModel().getColumn(0).setMaxWidth(50);
        /* Este es valido si se quiere seleccionar cada columna por separado */
//        t.getColumnModel().getColumn(1).setCellRenderer(new TableLabel(Color.LIGHT_GRAY));
        TableLabel [] labels = {new TableLabel("Andres"),
                                new TableLabel("Carlos"),
                                new TableLabel("Arturo"),
                                new TableLabel("Alex"),
                                new TableLabel("Hector"),
                                new TableLabel("Gajo"),
                                new TableLabel("Uribe"),
                                new TableLabel("Quique")};
        t.setRowHeight(50);
        for (TableLabel L : labels) {
            L.setBackground(Color.BLACK);
            L.setForeground(Color.WHITE);
            L.setOpaque(true);
            L.setPreferredSize(new Dimension(200,50));
            L.setHorizontalAlignment(JLabel.CENTER);
            L.setBorder(BorderFactory.createLineBorder(Color.green, 2));
            L.setIcon(RawLoader.getRawImage(RawLoader.hordeIcon));
            Object [] o = {false,L};
            tm.addRow(o);
        }
        t.setShowGrid(false);
        t.setRowMargin(5);
        JScrollPane sc = new JScrollPane(t);
        sc.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        f.add(sc, BorderLayout.CENTER);
        f.setVisible(true);
    }

    private static void testEditor () {
        Editor ed = new Editor("ES");
    }

    private static void testMethods(){
        methodsTest.test();
    }

    private static void testStack () {
        ArrayList<JLabel> labels = new ArrayList<JLabel>();
        for (int i = 0; i < 6; i++) {
            JLabel L = new JLabel("Label number: " + i);
            L.setPreferredSize(new Dimension(150,30));
            L.setBorder(BorderFactory.createLineBorder(Color.black));
            L.setHorizontalAlignment(JLabel.CENTER);
            labels.add(L);
        }
        HFrame f = new HFrame("Test Stack", 400,400);
        f.setLayout(new StackLayout());

        f.add(labels.get(0), StackLayout.putBesides);
        f.add(labels.get(1), StackLayout.putBesides);
        f.add(labels.get(2), StackLayout.putAndExpand);
        f.add(labels.get(3), StackLayout.putBelow);
        f.add(labels.get(4), StackLayout.putBesides);
        
//        f.add(labels.get(5), StackLayout.putAndExpand);
        f.setVisible(true);
    }

    private static void testIndependent() {
        tracesPanel revolution = (tracesPanel) HorrorControl.getInstance().createGraphics().getTracesPanel();
        revolution.enableLoader();
        HFrame newAge = new HFrame("Traces Loader - Revolution Succeed", 500,500,revolution);
        newAge.setVisible(true);

    }

    private static void testNetBots () {
        /* Full test */
        String location = "/horde/minefield/andres/netCheck/bots.xml";
        MasterAgent master = Launcher.start(location);
        boolean agentsReady = master.startAgents();
        master.post(Msg.composeRequest("test", "mrJediMaster", ""));
    }

    private static void testNetBotsSith() {
         String location = "/horde/minefield/andres/netCheck/botsSith.xml";
        MasterAgent master = Launcher.start(location);
        boolean agentsReady = master.startAgents();
        master.post(Msg.composeRequest("test", "mrLordSith", ""));
    }
}
